#!/bin/sh

# (C) Copyright 2006 International Business Machines
# This file is distributed under the Eclipse Public License
#
# $Id$
#
# Author: Andreas Waechter      IBM    2006-03-31

srcdir="@srcdir@"

retval=0

# add path for Matlab's MA57 library, if we use it
# for some reasons, the path could not be stored in the Ipopt
# library via -rpath
if test -n "@MWMA57PATH@" ; then
  export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:@MWMA57PATH@"
  export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:@MWMA57PATH@"
fi

echo " "
echo Running unitTests...
echo " "

# AMPL Solver executable
echo Testing AMPL Solver Executable...
if test -x ../src/Apps/AmplSolver/ipopt ; then
  if test -f mytoy.nl; then
    remove_mytoy=no
  else
    cp "$srcdir/mytoy.nl" .
    remove_mytoy=yes
  fi
  ../src/Apps/AmplSolver/ipopt mytoy.nl >tmpfile 2>&1
  grep "EXIT: Optimal Solution Found." tmpfile 1>/dev/null 2>&1
  if test $? = 0; then
    echo "    Test passed!"
  else
    retval=-1
    echo " "
    echo " ---- 8< ---- Start of test program output ---- 8< ----"
    cat tmpfile
    echo " ---- 8< ----  End of test program output  ---- 8< ----"
    echo " "
    echo "    ******** Test FAILED! ********"
    echo "Output of the test program is above."
  fi
  rm -rf tmpfile  mytoy.sol
  if test "$remove_mytoy" = "yes"; then
    rm -rf mytoy.nl
  fi
else
  echo "    no AMPL solver executable found, skipping test..."
fi

# C++ Example
echo Testing C++ Example...
./hs071_cpp >tmpfile 2>&1
grep "EXIT: Optimal Solution Found." tmpfile 1>/dev/null 2>&1
if test $? = 0; then
  echo "    Test passed!"
else
  retval=-1
  echo " "
  echo " ---- 8< ---- Start of test program output ---- 8< ----"
  cat tmpfile
  echo " ---- 8< ----  End of test program output  ---- 8< ----"
  echo " "
  echo "    ******** Test FAILED! ********"
  echo "Output of the test program is above."
fi
rm -rf tmpfile

# C Example
echo Testing C Example...
./hs071_c >tmpfile 2>&1
grep "EXIT: Optimal Solution Found." tmpfile 1>/dev/null 2>&1
if test $? = 0; then
  echo "    Test passed!"
else
  retval=-1
  echo " "
  echo " ---- 8< ---- Start of test program output ---- 8< ----"
  cat tmpfile
  echo " ---- 8< ----  End of test program output  ---- 8< ----"
  echo " "
  echo "    ******** Test FAILED! ********"
  echo "Output of the test program is above."
fi
rm -rf tmpfile

# Fortran Example
if test -e ./hs071_f ; then
echo Testing Fortran Example...
./hs071_f >tmpfile 2>&1
grep "EXIT: Optimal Solution Found." tmpfile 1>/dev/null 2>&1
if test $? = 0; then
  echo "    Test passed!"
else
  retval=-1
  echo " "
  echo " ---- 8< ---- Start of test program output ---- 8< ----"
  cat tmpfile
  echo " ---- 8< ----  End of test program output  ---- 8< ----"
  echo " "
  echo "    ******** Test FAILED! ********"
  echo "Output of the test program is above."
fi
rm -rf tmpfile

else
echo "Skip testing Fortran Example (hs071_f not available)"
fi


# clean up
rm -rf tmpfile debug.out ipopt.out IPOPT.OUT

exit $retval
